package com.scalar.chiptrack.operations.process.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.BomPartEntryDAO;
import com.scalar.chiptrack.operations.process.forms.BomReportForm;
import com.scalar.chiptrack.operations.process.BomPartForReport;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * User: Murali Krishna Yengala
 * Date: May 13, 2005
 * Time: 12:54:02 PM
 */
public class BomReportAction  extends ChipTrackAction{

    private static final String ACTION_NAME = "/bomReport.do" ;

    public ActionForward execute( ActionMapping mapping, ActionForm form,
                                  HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        HttpSession session = request.getSession();
        User user           = (User)session.getAttribute (SessionKeys.USER_KEY);
        HashMap<String, String> errors      = new HashMap<String, String> ();

        if (user == null)
        {
            session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
            return (mapping.findForward ("login"));
        }

        String action       = request.getParameter("action");
        action = action == null ? "" : action.trim();
        BomReportForm bomReportForm = (BomReportForm)  form;

        if ( action.equalsIgnoreCase("showReport") )
        {
            int maxAssemblyCanBeDone = -1;
            ArrayList bomPartsList = BomPartEntryDAO.getBomPartsForAssembly( bomReportForm.getAssemblyProcessStepId());
            Iterator iter = bomPartsList.iterator();
            while( iter.hasNext())
            {
                BomPartForReport bomPart = (BomPartForReport) iter.next();
                int qtyAvailable = Integer.parseInt(bomPart.getQtyAvailable());
                int qtyReq = Integer.parseInt( bomPart.getQtyReqPerUnit());
                int tmp = (int)Math.floor(qtyAvailable/qtyReq);
                if ( maxAssemblyCanBeDone == -1 || tmp < maxAssemblyCanBeDone )
                {
                    maxAssemblyCanBeDone = tmp;
                    if ( maxAssemblyCanBeDone == 0 )
                    {
                        break;
                    }
                }
            }
            iter = bomPartsList.iterator();
            while( iter.hasNext())
            {
                BomPartForReport bomPart = (BomPartForReport) iter.next();
                int qtyReq = Integer.parseInt( bomPart.getQtyReqPerUnit());
                int qtyToPull = maxAssemblyCanBeDone * qtyReq;
                bomPart.setQtyToPull( ""+qtyToPull );
            }
            bomReportForm.setMaxAssembly( ""+ maxAssemblyCanBeDone );
            if( bomPartsList.size() > 0 )
            {
                request.setAttribute("bomPartsList", bomPartsList);
                request.setAttribute ( "showDetails" , "TRUE" );
            }
            else
            {
                errors.put("No BOM parts are associated with this Assembly part number",null);
            }
            request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);
        }
        else if ( action.equalsIgnoreCase("bomInventoryReport") )
        {
            try
            {
                if( bomReportForm.getBomPartNumberId() == -1 )
                {
                    bomReportForm.setBomPartNumberId(0);
                }
                if (!"0".equalsIgnoreCase(bomReportForm.getProcessType() ) )
                {
                    bomReportForm.setBomPartNumberList( BomPartEntryDAO.loadAllBomPartNumbers( bomReportForm.getProcessType() , "-1") );
                }
                bomReportForm.setBomInventoryPartNumberList( BomPartEntryDAO.loadBomPartNumbersInventory( bomReportForm.getBomPartNumberId() ) );
                request.setAttribute("processTypes", getProcessTypes());
            }
            catch( Exception e ) { e.printStackTrace(); }

            return mapping.findForward("bomInventoryReport");
        }
        request.setAttribute("assemblyParts", ProductDao.getPartNumber("Assembly"));
        return mapping.findForward("bomReport");
    }

    private ArrayList<LabelValueBean> getProcessTypes()
    {
        ArrayList<LabelValueBean> processTypes = new ArrayList<LabelValueBean>();
        processTypes.add(new LabelValueBean(Constants.LOCATION_TYPE_DIE_BANK, Constants.LOCATION_TYPE_DIE_BANK));
        processTypes.add(new LabelValueBean(Constants.LOCATION_TYPE_FINISHED_GOODS, Constants.LOCATION_TYPE_FINISHED_GOODS));
        return processTypes;
    }
}
